#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>

#include <config.h>

#ifdef BUZZER_SUPPORT

#include <types.h>
#include <systems.h>
#include <drivers/sound/buzzer.h>

int buzzer_fd;
struct buzzer def_buzzer = { 25000, 2 };

inline int buzzer_init(void)
{
    buzzer_fd = open("/dev/buzzer", O_RDWR);
    if(buzzer_fd < 0) {
        ERROR("Can't open buzzer device\n");
        return -1;
    }
    
    return 0;
}

inline void buzzer_exit(void)
{
    if(buzzer_fd < 0)
        return;
    close(buzzer_fd);
}

inline int buzzer_write(struct buzzer *bz)
{
    if(buzzer_fd < 0)
        return 0;
        
    if(!bz)
        return -1;
        
    if(write(buzzer_fd, bz, sizeof(bz)) < 0)
        return -2;
        
    return 0;
}

inline int buzzer_read(struct buzzer *bz)
{
    if(buzzer_fd < 0)
        return 0;
        
    if(!bz)
        return -1;
        
    if(read(buzzer_fd, bz, sizeof(bz)) < 0)
        return -1;
        
    return 0;
}

#endif
